package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.StudentTaskRequestDTO;
import com.example.demo.mapper.StudentTaskManageMapper;
import com.example.demo.mapper.entity.AdvertiserView;
import com.example.demo.mapper.entity.StudentTask;
import com.example.demo.mapper.entity.StudentTaskView;
import com.example.demo.service.StudentTaskManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentTaskManageServiceImpl extends ServiceImpl<StudentTaskManageMapper, StudentTask> implements StudentTaskManageService {
    @Autowired
    private StudentTaskManageMapper mapper;

    @Override
    public PageVo<StudentTaskView> getStudentPicTaskList(PageBo pageBo) {
        List<StudentTaskView> logs = mapper.getStudentPicTaskList();
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<StudentTaskView> getStudentQRTaskList(PageBo pageBo) {
        List<StudentTaskView> logs = mapper.getStudentQRTaskList();
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<StudentTaskView> getStudentCameraTaskList(PageBo pageBo) {
        List<StudentTaskView> logs = mapper.getStudentCameraTaskList();
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<StudentTaskView> getStudentQuizTaskList(PageBo pageBo) {
        List<StudentTaskView> logs = mapper.getStudentQuizTaskList();
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<StudentTaskView> searchStudentTask(StudentTaskView request, PageBo pageBo) {
        String taskName = request.getTaskName();
        int completionStatus = request.getCompletionStatus();
        int studentId = request.getStudentId();
        int taskAllocation = request.getTaskAllocation();

        List<StudentTaskView> list = mapper.getStudentPicTaskList();
        //按照編號，姓名搜索
        list = list.stream()
                .filter(o -> (taskName == null || o.getTaskName().contains(taskName))&&
                        (completionStatus == 0 || o.getCompletionStatus() == completionStatus)&&
                        (studentId == 0 || o.getStudentId() == studentId)&&
                        (taskAllocation == 0 || o.getTaskAllocation() == taskAllocation)).collect(Collectors.toList());
        return new PageVo<>(pageBo, list);
    }

    @Override
    public List<StudentTaskView> getEdutaskDetails(int id) {
        return mapper.getEdutaskDetails(id);
    }

    @Override
    public Result<String> updateStudentTask(StudentTask request) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime lastUpdatedTime = LocalDateTime.now();
        String format = lastUpdatedTime.format(formatter);

        StudentTask newStudentTask = mapper.selectById(request.getId());
        newStudentTask.setCompletionStatus(request.getCompletionStatus());
        if(request.getCompletionStatus()==2) newStudentTask.setCompletionTime(format);
        if (newStudentTask != null) {
            int result = mapper.updateById(newStudentTask);
            if (result > 0) {
                return Result.ok("修改成功");
            } else {
                return Result.fail("修改失败");
            }
        } else {
            return Result.fail("参数不正确或未提供主键信息");
        }
    }



}
